﻿//------------------------------------------------------------------------------ 
// <copyright file="JConfirm.cs" company="Fm Computer">
//     Copyright (c) Fm Computer di Menatti Fulvia.  All rights reserved.
// </copyright>
//----------------------------------------------------------------------------- 
namespace FmjQuery
{
    using System;
    using System.ComponentModel;
    using System.Text;
    using System.Web.UI;

    /// <summary>
    /// Add confirm question to control
    /// </summary>
    [ToolboxData("<{0}:JConfirm runat=server SubmitServer=true SubmitClient=true />")]
    [DefaultProperty("TargetControlID")]
    [NonVisualControl]
    public class JConfirm : JQueryExtendControl
    {
        #region Costruct
        /// <summary>
        /// Initializes a new instance of the JConfirm class.
        /// </summary>
        public JConfirm()
        {
            this.IsRenderControl = false;
        }
        #endregion

        #region Property
        /// <summary>
        /// Gets or sets Question
        /// </summary>
        [Description("Text of question")]
        public string QuestionText
        {
            get; set;
        }

        /// <summary>
        /// Gets or sets name of client success function
        /// </summary>
        [Description("Name of client function that execute when user click ok")]
        public string ClientSuccessFunction
        {
            get; set;
        }

        /// <summary>
        /// Gets or sets name of client cancel function
        /// </summary>
        [Description("Name of client function that execute when user click cancel")]
        public string ClientCancelFunction
        {
            get; set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether execute client function
        /// </summary>
        [DefaultValue("true")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [Description("Indicates if execute client function write into Target Control")]
        public bool SubmitClient
        {
            get; set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether execute server function
        /// </summary>
        [DefaultValue("true")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [Description("Indicates if execute server function")]
        public bool SubmitServer
        {
            get; set;
        }
        #endregion

        #region Overrides of JQueryExtendControl
        /// <summary>
        /// Return list of webResource
        /// </summary>
        /// <returns>web resources</returns>
        public override JWebSources GetResources()
        {
            return null;
        }

        /// <summary>
        /// Gets list of css image resources
        /// </summary>
        /// <returns>list of images</returns>
        public override CssResources GetCssResources()
        {
            return null;
        }

        /// <summary>
        /// Gets list of client nameSpace
        /// </summary>
        /// <returns>list of namespace</returns>
        public override ClientNameSpaces GetClientNameSpace()
        {
            return null;
        }

        /// <summary>
        /// Create jQuery control
        /// </summary>
        public override void CreateJControl()
        {
            this.Manager.FrameWorkExtend = true;
            if (this.Enabled)
            {
                // check param
                if (string.IsNullOrEmpty(this.QuestionText))
                {
                    throw new InvalidOperationException("You must insert Question");
                }

                // create script
                StringBuilder builder = new StringBuilder();

                builder.AppendFormat("$('#{0}').createConfirm(", this.TargetControlClientID);
                builder.Append("{");
                builder.AppendFormat("questionText:'{0}',", this.QuestionText.ParseStringToClient());

                if (!string.IsNullOrEmpty(this.ClientSuccessFunction))
                {
                    builder.AppendFormat("clientSuccess:{0},", this.ClientSuccessFunction);
                }

                if (!string.IsNullOrEmpty(this.ClientCancelFunction))
                {
                    builder.AppendFormat("clientCancel:{0},", this.ClientCancelFunction);
                }

                builder.AppendFormat("submitClient:{0},", this.SubmitClient.ToString().ToLower());
                builder.AppendFormat("submitServer:{0}", this.SubmitServer.ToString().ToLower());
                builder.Append("});");

                Utility.RegisterStartUpScript(Page, string.Concat("confirm_", this.ClientID), builder.ToString());
            }
        }
        #endregion
    }
}
